package net.mattjoyce.buzzwordbingo;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ConfigScreen extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {
	public static final String KEY_EDITTEXT_PREF = "editTextPref";
	private EditTextPreference mEditTextPreference;
	private Button addButton;
	private Button removeButton;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
    
		addPreferencesFromResource(R.xml.config_screen);
		setContentView(R.layout.config_layout);

		mEditTextPreference = (EditTextPreference) getPreferenceScreen().findPreference(KEY_EDITTEXT_PREF);
		removeButton = (Button) findViewById(R.id.btn_cfg_remove);
		addButton = (Button) findViewById(R.id.btn_cfg_add);
	}

	@Override
    protected void onResume() {
		super.onResume();
		mEditTextPreference.setSummary("Current value is " + PreferenceManager.getDefaultSharedPreferences(this).getString(KEY_EDITTEXT_PREF, ""));
		PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
		addButton.setOnClickListener(this);
		removeButton.setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		Preference pref = findPreference(key);

	    if (pref instanceof EditTextPreference) {
	        //EditTextPreference editTextPref = (EditTextPreference) pref;
	        pref.setSummary("Current value is " + sharedPreferences.getString(key,""));
	    }
	}
	
    public void onClick(View v) {
    	switch(v.getId()) {
    	case R.id.btn_cfg_add:
    		//do something
    		Toast.makeText(getApplicationContext(), "Hello ADDButton", Toast.LENGTH_SHORT).show();
    		break;
    	case R.id.btn_cfg_remove:
    		//do something else
    		Toast.makeText(getApplicationContext(), "Hello RemoveButton", Toast.LENGTH_SHORT).show();
    		break;
    	}
    	
    }
}